﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.IO;
using mesoBoard.Data;
using mesoBoard.Data.Repositories;
using System.Drawing.Imaging;
using System.Drawing;

namespace mesoBoard.Services
{
    public class Files : IFiles
    {
         
        private IRepositoriesWrapper Repositories;

        public Files(IRepositoriesWrapper repos)
        {
            this.Repositories = repos;
        }

        public void CreateAttachment(HttpPostedFileBase file, int PostID)
        {
            string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload/Files"), file.FileName);

            string savedName = file.FileName;

            if (File.Exists(filePath))
                savedName = Randoms.CleanGUID() + file.FileName;

            file.SaveAs(HttpContext.Current.Server.MapPath(Path.Combine("~/Upload/Files", savedName)));

            Repositories.Attachments.CreateAttachement(new Attachment
            {
                Downloaded = 0,
                SavedName = savedName,
                DownloadName = file.FileName,
                PostID = PostID,
                Size = file.ContentLength,
                Type = file.ContentType
            });
        }

        public IEnumerable<Attachment> GetPostAttachments(int postID)
        {
            return Repositories.Attachments.GetPostAttachments(postID);
        }

        public void CreateAttachments(HttpFileCollectionBase files, int postID)
        {
            foreach (string filename in files)
            {
                HttpPostedFileBase f = files[filename];
                if (f.ContentLength > 0)
                    CreateAttachment(f, postID);
            }
        }

        public void DeleteAttachment(int attachmentID)
        {
            Attachment TheAttachment = Repositories.Attachments.GetAttachment(attachmentID);
            string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload/Files"), TheAttachment.SavedName);
            if (System.IO.File.Exists(filePath))
                System.IO.File.Delete(filePath);
            Repositories.Attachments.DeleteAttachment(attachmentID);
        }

        public void DeleteAttachments(int[] attachmentIDs)
        {
            foreach (int i in attachmentIDs)
                DeleteAttachment(i);
        }

        public void UploadAvatar(HttpPostedFileBase file, User TheUser)
        {
            string random_name = Randoms.CleanGUID() + file.FileName.Remove(file.FileName.IndexOf('.')) + ".gif";
            string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Upload/Avatars"), random_name);
            Image ravatar = Image.FromStream(file.InputStream, true, true);
            if (ravatar.Width > int.Parse(SiteConfig.AvatarWidth.Value) || ravatar.Height > int.Parse(SiteConfig.AvatarHeight.Value))
            {
                ravatar = ravatar.GetThumbnailImage(int.Parse(SiteConfig.AvatarWidth.Value), int.Parse(SiteConfig.AvatarHeight.Value), null, IntPtr.Zero);
            }
            ravatar.Save(filePath, ImageFormat.Gif);

            TheUser.UserProfile.Avatar = random_name;
        }
    }
}